<?php
                include("global.php");
                printHeader(false);
                session_start();
                
                $LISTA_RUBRICA_SMS=$_SESSION['LISTA_RUBRICA_SMS'];
                $LISTA_RUBRICA_EMAIL=$_SESSION['LISTA_RUBRICA_EMAIL'];
                $LISTA_RUBRICA_FAX=$_SESSION['LISTA_RUBRICA_FAX'];
                $LISTA_ALR_ENABLE_SMS=$_SESSION['LISTA_ALR_ENABLE_SMS'];
                $LISTA_ALR_ENABLE_FAX=$_SESSION['LISTA_ALR_ENABLE_FAX'];
                $LISTA_ALR_ENABLE_EMAIL=$_SESSION['LISTA_ALR_ENABLE_EMAIL'];
                $key = @urldecode($_GET["key"]); 
                $matches=explode("&",$key);
                $rubrica=explode("=",$matches[0]);
                $op=explode("=",$matches[1]);
                $rubrica=$rubrica[1];
                $op=$op[1];
//                $rubrica="sms";
//                $op="mod";
                
                ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="it">
    <head>
        <style>
        body {
                    text-align: left;
            }
/* error message */
.error {
	height:15px;
	background-color:#FFFE36;
	font-size:11px;
	border:1px solid #E1E16D;
	padding:4px 10px;
	color:#000;
	display:none;	
	
	-moz-border-radius:4px;
	-webkit-border-radius:4px; 
	-moz-border-radius-bottomleft:0;
	-moz-border-radius-topleft:0;	
	-webkit-border-bottom-left-radius:0; 
	-webkit-border-top-left-radius:0;
	
	-moz-box-shadow:0 0 6px #ddd;
	-webkit-box-shadow:0 0 6px #ddd;
}
h2
{
    color: #15428B;
}

</style>
        <link media="screen" type="text/css" href="style/form.css" rel="stylesheet" />
	<link media="screen" type="text/css" href="style/formcheck.css" rel="stylesheet" /> 
        <link rel="stylesheet" type="text/css" href="style/button.css" />
       
        <script type="text/javascript" src="files/js/core.js"></script>
	<script type="text/javascript" src="files/js/more.js"></script>
        <script type="text/javascript" src="files/js/lang/it.js"></script> 
        <script src="files/js/jquery-1.4.2.js" type="text/javascript"></script>
<!--	<script type="text/javascript" src="files/js/jquery.tools.min.js"></script> -->
        <script type="text/javascript" src="files/js/formcheck.js"></script> 
        
        <script type="text/javascript">
                var num_select=0;
                var clickDueVolte=0;

                var avLockCheck = null;

                function pageInitial() {
                    avLockCheck = new FormCheck('formular');
                }

               // window.addEvent('domready', pageInitial);
                
                
                $(document).ready(function() {
                // se il checkbox è selezionato coloro la label per simulare la colorazione delle select
                $('#jqmultiselect label').each(function() {
                if ($(this).find(':checkbox').attr('checked'))                   
                    $(this).addClass('selected');
                    
                });
                // al click sul checkbox metto/tolgo la classe 'selected'
                $('#jqmultiselect :checkbox').click(function(e) {
                var checked = $(this).attr('checked');
                $(this).closest('label').toggleClass('selected', checked);
                
                });
                });
            
                function selectCkeck(ob)
                {
                   if(ob.checked)
                       num_select++;
                   else
                       num_select--;
                }
                function validateEmail(email) { 
                    var re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
                    return re.test(email);
                } 
                function invioForm(rubrica,operation)
                {
                    
                    var oForm = document.forms['formular'];
                    var nome=oForm.elements["nome"].value;
                    var azienda=oForm.elements["azienda"].value;
                    var destinatario=oForm.elements["destinatario"].value;
                    
                    if( (nome=='' || azienda=='' || destinatario=='') ||
                        ((num_select <= 0) && ("sms" != rubrica))   ||
                        //numero telefono fax e sms
                        (  (rubrica =='fax' || rubrica == 'sms') && 
                            (!  (! isNaN(destinatario) && (destinatario.length >= 10))) ) ||
                        (  (rubrica =='email') && !(validateEmail(destinatario ) ) )
                      
                      )
                    {
                        $("#nessunaSelect").attr('class', 'message warning');
                        $("#nessunaSelect").html("Attenzione: bisogna compilare tutti i campi secondo il formato richiesto");
                    }
                    else
                        document.forms['formular'].submit(); 
                }
        </script>
        
        <script type="text/javascript">
        
//        window.addEvent('domready', function(){
//            new FormCheck('formular');
//        });

        </script>
        
         <style type="text/css">
            div.message{margin: 0 0  2em; padding: 15px 10px 0 50px;border: solid;border-width: 2px 0;background:#F0F0F0}
            div.message p{margin: 0 0 1em}
            div.message.warning{background: #FFF0BA url(img/warning20.png) no-repeat 20px 3px;border-color: #E87C29}
            
            
            ul#jqmultiselect {
            border: 2px solid #B5B8C8;
            height: 70px;
            list-style: none outside none;
            overflow-x: hidden;
            overflow-y: auto;
            padding: 0;
            width: 628px;
            background-color: #ffffff;
            }
            #jqmultiselect li label {
            color: #000000;
            display: block;
            outline: medium none;
            padding: 4px 4px 4px 26px;
            position: relative;
            }
            #jqmultiselect li label:hover {
            background-color: #b1a9a9;
            color: #000000;
            }
            #jqmultiselect li label.selected {
            background-color: #d0cece;
            color: #9A9191;
            }
            #jqmultiselect li label .checkbox {
            left: 2px;
            position: absolute;
            top: 2px;
            }
        </style>
        
    </head>
    
    <body>
                <?php
                  
                   
                   $str_dest="Numero Tel:";
                   $ARRAY=$LISTA_RUBRICA_SMS;
                   $ARRAY_ALLARM=$LISTA_ALR_ENABLE_SMS;
                   
                   $check="class=\"validate['required','length[10,-1]','digit'] text-input\" ";
                   $pattern="pattern=\"[0-9]{10,}\" "; //cellulare
                   
                   if("email" == $rubrica)
                   {
                        $str_dest="Indirizzo Email:";
                        $check="class=\"validate['required','length[5,-1]','digit'] text-input\"";
                        $pattern="pattern=\"[a-z._-]+\@[a-z._-]+\.[a-z]{2,4}\" "; //email
                        $ARRAY=$LISTA_RUBRICA_EMAIL;
                        $ARRAY_ALLARM=$LISTA_ALR_ENABLE_EMAIL;
                   }
                   else if("fax" == $rubrica)
                   {
                       $ARRAY=$LISTA_RUBRICA_FAX;
                       $ARRAY_ALLARM=$LISTA_ALR_ENABLE_FAX;
                   }
                   
                   if("new"  == $op) //nuovo
                   {
                        print("<h2>Inserimento nuovo utente in rubrica $rubrica</h2>");
                        print("
                                <form name=\"formular\" class=\"formular\" id=\"formular\" method=\"post\" action=\"files/php/rubricaOperation.php\">
                                    
                                <fieldset> 				<legend>Nuovo Utente</legend> 				
                                        <label> 					
                                            <span>Nome : </span>
                                            <input type=\"text\" class=\"validate['required','length[3,-1]','nodigit'] text-input\" name=\"nome\" required=\"required\" pattern=\"[a-zA-Z ]{3,}\"/> 				
                                        </label>
                                        <label>
                                            <span>Azienda : </span>
                                            <input type=\"text\" class=\"validate['required','length[3,-1]','alphanum'] text-input\" name=\"azienda\" name=\"user\" required=\"required\" pattern=\".{3,}\"/>
                                        </label>
                                        <label>
                                            <span>$str_dest  </span> 
                                            <input type=\"text\" $check name=\"destinatario\" required=\"required\" $pattern />
                                        </label>
                                <div id=\"nessunaSelect\" ></div>
                                ");
                                 if("sms" != $rubrica)
                                 {
                                        print("<label for=\"phone\">ID Allarme</label>
                                         <ul id=\"jqmultiselect\">");
                        
                                        foreach($ARRAY_ALLARM as $i => $tel)
                                        {
                                            print("<li>
                                                    <label for=\"enableAlarms_$i\">
                                                  ");
                                            print("<input type=\"checkbox\" name=\"enableAlarms[]\" id=\"cinema\" value=\"$i\" class=\"checkbox\" onClick=\"javascript:selectCkeck(this);\"/>");
                                            print("$i - $tel</label></li>");
                                            
                                        }
                                        print("</ul></label>");
                                 }
                        print("
                                <div class=\"button\"> 
                                    <input type=\"button\" value=\"Salva\" class=\"buttonview\" onClick=\"javascript:invioForm('$rubrica','$op');\"/> 
                                </div>
                                <div class=\"cancel_$rubrica\"> 
                                    <input type=\"reset\"  value=\"Reset\" class=\"buttonglosalva\" /> 			
                                </div>
                        ");
//                        print("<fieldset><legend>Nuovo Elemento</legend> ");
//                        print("<form method=\"post\" action=\"rubricaOperation.php\" class=\"formular\" id=\"formular\">\n");
//                        print("<label><span>Nome:</span><input type=\"text\" name=\"nome\" class=\"validate['required','length[3,-1]','nodigit'] text-input\" required=\"required\" pattern=\"[a-zA-Z ]{3,}\"/></labal>\n");
//                        print("<label><span>Azienda:</span><input type=\"text\" class=\"validate['required','length[3,-1]','nodigit'] text-input\" name=\"azienda\" value=\"azienda\" required=\"required\" minlength=\"5\"/></labal>\n");
//                        print("<label><span>$str_dest<span><input type=\"text\" class=\"validate['required','length[3,-1]','nodigit'] text-input\" name=\"destinatario\" value=\"destinatario\" required=\"required\" pattern=\"[a-zA-Z ]{5,}\"/></labal>\n");      
                        print("<input type=\"hidden\" name=\"operation\" value=\"new\"/>\n");
                        print("<input type=\"hidden\" name=\"rubrica\" value=\"$rubrica\"/>\n");
                       // print("<div class=\"button\"> <input type=\"submit\" value=\"Aggiungi\" class=\"submit\" /> </div>");
                        print("</fieldset></form>");

                  }
                  else if("mod"  == $op) //modifcia o elimina
                  {

                        $id=explode("=",$matches[4]);
                        $id=$id[1]; //key destinatario numero
                        $arr=$ARRAY[$id];
                        $lista=$arr[3];
                       
                        print("<h2>Modifica utente in rubrica $rubrica</h2>");
                        print("
                                <form class=\"formular\" id=\"formular\" method=\"post\" action=\"files/php/rubricaOperation.php\">
                                <fieldset> 				<legend>Informazioni Utente</legend> 				
                                        <label> 					
                                            <span>Nome : </span>
                                            <input type=\"text\" value=\"$arr[0]\" class=\"validate['required','length[3,-1]','alphanum'] text-input\" name=\"nome\" required=\"required\" pattern=\"[a-zA-Z ]{3,}\"/> 				
                                        </label>
                                        <label>
                                            <span>Azienda : </span>
                                            <input type=\"text\" value=\"$arr[1]\" class=\"validate['required','length[3,-1]','alphanum'] text-input\" name=\"azienda\" name=\"user\" required=\"required\" pattern=\".{3,}\"/>
                                        </label>
                                        <label>
                                           <span>$str_dest  </span> 
                                            <input type=\"text\" $check value=\"$id\" name=\"destinatario\" required=\"required\" $pattern/>
                                        </label>
                                <div id=\"nessunaSelect\" ></div>
                                ");
                                 if("sms" != $rubrica)
                                 {
                                     print("<label for=\"phone\">ID Allarme</label>
                                         <ul id=\"jqmultiselect\">");
                                         foreach($ARRAY_ALLARM as $i => $tel)
                                         {
                                                $selected="";
                                                if($lista[$i])
                                                    $selected="checked";
                                                print("<li>
                                                        <label for=\"enableAlarms_$i\">
                                                      ");
                                                print("<input type=\"checkbox\" name=\"enableAlarms[]\" id=\"cinema\" value=\"$i\" class=\"checkbox\" $selected onClick=\"javascript:selectCkeck(this);\"/>");
                                                print("$i - $tel</label></li>");

                                         }    
                                     print("</ul></label>");
                                 }
                        
                        print("
                                <div class=\"button\"> 
                                    <input type=\"button\" value=\"Salva\" class=\"buttonview\" onClick=\"javascript:invioForm('$rubrica','$op');\"/> 
                                </div>
                                <div class=\"cancel_$rubrica\"> 
                                    <input type=\"reset\"  value=\"Reset\" class=\"buttonglosalva\" /> 			
                                </div>
                        ");
                        print("<input type=\"hidden\" name=\"rubrica\" value=\"$rubrica\"></input>");
                        print("<input type=\"hidden\" name=\"id\" value=\"$id\"></input>");
                        print("<input type=\"hidden\" name=\"destinatario_old\" value=\"$id\"></input>");
                        print("<input type=\"hidden\" name=\"operation\" value=\"mod\"></input>");
                        
                        print("</form></fieldset>");
                  }
                  else if("del" == $op) 
                  {
                        $id=explode("=",$matches[4]);
                        $id=$id[1];
                        $arr=$ARRAY[$id];
                        print("<h2>Elimina utente in rubrica $rubrica</h2>");            
                        print("
                                <form class=\"formular\" id=\"formular\" method=\"post\" action=\"files/php/rubricaOperation.php\">
                                    <fieldset> 				<legend>Informazioni Utente</legend> 				
                                        <label> 					
                                            <span>Nome : </span>
                                            <input type=\"text\" value=\"$arr[0]\" readonly/> 				
                                        </label>
                                        <label>
                                            <span>Azienda : </span>
                                            <input type=\"text\" value=\"$arr[1]\" readonly/>
                                        </label>
                                        <label>
                                            <span>$str_dest  </span> 
                                            <input type=\"text\" value=\"$id\" name=\"destinatario\" readonly/>
                                        </label> 
                                    <br/>
                        ");
                        
                        print("<input type=\"hidden\" name=\"rubrica\" value=\"$rubrica\"></input>");
                        print("<input type=\"hidden\" name=\"operation\" value=\"del\"></input>");
//                        print("<div class=\"infos\">Selezionare per eliminare l'utenete anche dalla rubrica email se presente: </div>");
//                        print("<label><span class=\"checkbox\">Elimina: </span><input type=\"checkbox\" class=\"validate['required'] checkbox\" name=\"Elimina Email\"/></label>\n"); 
                        print("<input type=\"hidden\" name=\"operation\" value=\"del\"></input>");
                        print("<div class=\"button\"> 
                                    <input type=\"submit\" value=\"Elimina\" class=\"buttonview\" /> 
                                </div>
                              ");
                        print("</fieldset></form>");
                  }
              ?>
    <br />
<script>
//$("#formular").validator();
</script>

<!--    <h1>Validazione form</h1>
<form class="formular" id="formular" method="post" action="#">
    <fieldset> 				<legend>User informations</legend> 				
	<label> 					
            <span>Nome : </span>
            <input type="text" class="validate['required','length[6,16]','alphanum'] text-input" name="nome" required="required" pattern="[a-zA-Z ]{3,}"/> 				
	</label>
	<label>
            <span>Azienda : </span>
            <input type="text" class="validate['required','length[3,-1]','alphanum'] text-input" name="azienda" name="user" required="required" pattern="[a-zA-Z ]{3,}"/>
        </label>
        <label>
            <span>Numero Telefono : </span> 
            <input type="text" class="validate['required','length[3,-1]','digit'] text-input" name="destinatario" />
        </label> 
    <div class="button"> <input type="submit" value="Validate &amp; Send the form!" class="submit" /> <input type="reset" value="Reset" />			
    </div>
        </fieldset>
    
    <hr />-->

</body>
</html>
